<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/17-16:06
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\job;
    use think\queue\job;

    class Hello
    {

        /**
         * @param job $job  //当前的任务对象
         * @param $data //发布任务时自定义的数据
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17
         * @name: fire
         * @describe:
         */
        public function fire(job $job,$data){
            // 有些消息在到达消费者时,可能已经不再需要执行了
            $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);
            if(!$isJobStillNeedToBeDone){
                $job->delete();
                return;
            }
            $isJobDone = $this->doHelloJob($data);

            if ($isJobDone) {
                //如果任务执行成功， 记得删除任务
                print_r("<info>Hello Job has been done and deleted"."</info>\n");
                $job->delete();
            }else {
                if ($job->attempts() > 3) {
                    //通过这个方法可以检查这个任务已经重试了几次了
                    print_r("<warn>Hello Job has been retried more than 3 times!" . "</warn>\n");
                    $job->delete();
                }else{
                    // 重新发布这个任务
                    $job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
                }
            }
        }


        /**
         * @param $data
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17
         * @name: failed
         * @describe: 发送失败写入系统日志
         */
        public function failed($data){
            cache('failtime',time());
        }


        /**
         * @param $data //发布任务时自定义的数据
         * @return bool // 任务执行的结果
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17
         * @name: checkDatabaseToSeeIfJobNeedToBeDone
         * @describe: 有些消息在到达消费者时,可能已经不再需要执行了
         */
        private function checkDatabaseToSeeIfJobNeedToBeDone($data){
            return true;
        }


        /**
         * @param $data // 发布任务时自定义的数据
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17
         * @name: doHelloJob
         * @describe:根据消息中的数据进行实际的业务处理
         */
        private function doHelloJob($data) {
            // 根据消息中的数据进行实际的业务处理...
           file_put_contents('aaaa.txt',"123ASDASDADDDDDDDDDDDDDDD",FILE_APPEND);
           return true;
        }

    }